﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mathematics.Expressions;

namespace Mathematics
{
    public class PrintKeyword:Keyword
    {
        public PrintKeyword(MathExpression expression)
        {
            Expression = expression;
        }

        public MathExpression Expression { get; private set; }

        public override ExpressionValue Evaluate()
        {
            Print(Expression);
            return ExpressionValue.Void;
        }

        public static void Print(MathExpression expression)
        {
            string text;
            var canEvaluate = expression.CanEvaluate();
            if (!canEvaluate)
                text = "Unable to evaluate expression";
            else
            {
                var value = expression.Evaluate();
                if (value == null)
                    text = "null";
                else
                {
                    text = value.ToString();
                }
            }

            Console.WriteLine();
            Console.WriteLine(text);
        }
    }
}
